<?php 
session_start();

include "dbconnect.php";

$closeerr=$deleteerr="";
$cl=$dl="";
$hireUsername="";
if(!isset($_SESSION['Username']))
{
  header("location:index.php");

}
else 
{
	if($_SESSION['Type']!=1)
  header("location:unauthorized.php");
	//echo isset($_SESSION['Username']);
	
}
		$post=0;
		$result="";
		$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
		mysql_select_db($_DB_NAME,$con);
		$result1=mysql_query("select distinct year from semester order by year desc");//Need to depend on the selected semester
		
	if ($_SERVER["REQUEST_METHOD"] == "POST")
	{
			$cl=$_POST['closecourse'];
			$dl=$_POST['deletecourse'];
			
			$sem=$_POST["term"];
			$year="";

			mysql_query("SET @pterm='".$_POST["term"]."'");
			mysql_query("SET @pyear=".$_POST["year"]);
			$semidrs=mysql_query("select semid from semester where term=@pterm and year=@pyear");
			if($semidrs == false)
			{
				die(mysql_error());
			}
			$semid = mysql_fetch_assoc($semidrs);
			
				if(isset($_POST["closeall"])) //all close
					{
						if($_POST["closeall"] == 1)
						{
							mysql_query("SET @psemid=".$semid["semid"]);
							$openingidrs=mysql_query("UPDATE openings SET isclosed = 1 WHERE semid = @psemid;");
							
							if($openingidrs==false)
							{
								die(mysql_error());
								
							}	
							echo "<script>alert(\"Successfully done\")</script>";
							header('location:managesuccess.php');
						}
						else //single course close or delete
						{
							
							$clcourse=$_POST['closecourse'];
	
							if (empty($clcourse))
								{
									//delete
									if($_POST["deleteall"] == 1) //all delete
									{
									mysql_query("SET @psemid=".$semid["semid"]);
									$openingidrs=mysql_query("UPDATE openings SET isdeleted = 1 , isclosed=1 WHERE semid = @psemid;");
									if($openingidrs==false)
										{
											die(mysql_error());
										}
										echo "<script>alert(\"Successfully done\")</script>";
										header('location:managesuccess.php');	
									}
								else //single deleete
									{
										$dlcourse=$_POST['deletecourse'];
		
										if (empty($dlcourse))
											{
												$deleteerr = "Enter course";
											}
										else{
												$post=1;
												mysql_query("SET @pcourseid='".$_POST["deletecourse"]."'");
												mysql_query("SET @psemid=".$semid["semid"]);
												$openingidrs=mysql_query("UPDATE openings SET isdeleted = 1,isclosed=1 WHERE semid = @psemid and courseid=@pcourseid;");
														
												if($openingidrs==false)
												{
													die(mysql_error());
												}	
												echo "<script>alert(\"Successfully done\")</script>";
												header('location:managesuccess.php');
											}	
									}
								}
							else
								{	
								$post=1;
								
								mysql_query("SET @pcourseid='".$_POST["closecourse"]."'");
										mysql_query("SET @psemid=".$semid["semid"]);
										$openingidrs=mysql_query("UPDATE openings SET isclosed = 1 WHERE semid = @psemid and courseid=@pcourseid;");
											
				
										if($openingidrs==false)
										{
											die(mysql_error());
										}	
										header('location:managesuccess.php');
								}
			  				}
			  		}
				
	
		}

?>
	

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Manage</title>

<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>


 <link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />
    <link rel="stylesheet" href="jqwidgets/styles/jqx.summer.css" type="text/css" />
    <script type="text/javascript" src="scripts/jquery-2.0.3.min.js"></script>
    <script type="text/javascript" src="jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="jqwidgets/jqxexpander.js"></script>
    
  <script src="scripts/jquery-ui-1.10.3/jquery-ui-1.10.3/ui/jquery-ui.js" type="text/javascript" > </script>
  
  <script>
        $(document).ready(function () {
            // Create jqxExpander
            $("#expanderman").jqxExpander({});
        });
		
	</script>
    <script type="text/javascript">
		//function CourseSuggest() {
			//var courseidList = "";
			//var courseidValue =
			
					
        $(document).ready(function () {
            // Create jqxExpander
            $("#expanderexc").jqxExpander({}); 
			
        });
		</script>
        <script type="text/javascript">
		document.ready(function (){
		$("#sem").live('change', function (e){
			 window.location.href = 'index.php';
			});
		});
		
		</script>
        <script type="text/javascript">
		function courseSuggest()
		{
    				$.ajax({
						type: "POST",
                		url: "autoSuggest.php",
                		data: {option: 1, courseid: $("#closecourse").val()},
					    success: function(courses){
									$("#closecourse").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
			
		}
		function courseSuggest1()
		{
    				$.ajax({
						type: "POST",
                		url: "autoSuggest.php",
                		data: {option: 1, courseid: $("#deletecourse").val()},
					    success: function(courses){
									$("#deletecourse").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
			
		}
		function validate()
		{
			
			if(document.getElementById("year").value=="")
			{
				alert("Enter the year");
				return false;
			}
			else
			{
				if(document.getElementById("closeall").checked==false)
				{
					if(document.getElementById("closecourse").value=="")
					{
						alert("Enter the course");
						return false;
					}
					else
					{
						return true;
					}
				}
				else
				{
					return true;
				}
			}
			
		}
		function validate1()
		{
			if(document.getElementById("year").value=="")
			{
				alert("Enter the year");
				return false;
			}
			else
			{
				if(document.getElementById("deleteall").checked==false)
				{
					if(document.getElementById("deletecourse").value=="")
					{
						alert("Enter the course");
						return false;
					}
					else
					{
						return true;
					}
				}
				else
				{
					return true;
				}
			}
		}
		function populateYear()
		{
			$.ajax({
						type: "POST",
                		url: "PopulateYear.php",
                		data: {term :document.getElementById("term").value,year: document.getElementById("year").value},
					   success: function(courses){
									$("#year").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
		}
		</script>


<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>
<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />

</head>

<body style="min-width:1280px">

<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img  height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="grey">
</div>
<center>
<div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->

              <ul>
                 <li ><a href="hireView.php">Hire</a></li>
                <li ><a href="post.php">Openings</a></li>
                <li ><a href="budget.php">Budget</a></li>
                <li ><a href="semester.php">Semester</a></li>
                <li ><a href="manage.php">Manage</a></li>
              </ul>          

</div>
</center>
	<span style="width:100px;margin-left:300px;">
                <button class="hyperbutton" onclick="location.href = 'logout.php'" >logout</button>
         </span>
        <div class="footbody">

        <form method="post" autocomplete="off" action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"]))?>">
<table cellspacing="5" cellpadding="5">
    <thead>
    <tr>
        <td>
        <label class="Header1">Manage openings</label>
        </td>
    </tr>
    </thead>
    <tbody>
    <tr/>
    <tr/>
    <tr/>
   		 		<tr>
                        <td>
                    	<label class="crimson-text">Select a semester </label>
                      </td>
                  <td>
                      <select name="term" id="term" class="crimson-text" >
                        <option selected value="FALL" <?php if(isset($sem) and $sem=="FALL") echo "selected";?>>Fall</option>
                        <option value="SPRING" <?php if(isset($sem) and $sem=="SPRING") echo "selected";?>>Spring</option>
                      </select>
                  </td> 
   			 </tr>
                  <tr>
                      <td>
                        <label class="crimson-text">Year </label>
                      </td>
                  <td>
                  		<input type="text"  name="year" id="year" class="crimson-border" onkeyup="populateYear()"></select>
                  </td>
			</tr>
</tbody>
</table>

        <div id='expanderman'  style="margin-top:20px">
        <div>
        Close an Opening -- Students cannot apply
        </div>
        <div>

        <table cellspacing="5" cellpadding="5">
        <tbody>
        <tr/><tr/><tr/><tr/>
        <tr>
        <td/>	
        <td>
        <label class="crimson-text">Course</label>
        </td>
        <td>
       	<input type="text"	id="closecourse" name="closecourse" class="crimson-border" value="<?php echo $cl;?>" onkeyup="courseSuggest()"/>		
        </td>
        </td><td class="error" >
     <?php  echo $closeerr;?>
        </td>
        </tr>
        <tr/><tr/><tr/><tr/>
        <tr>
        <td/>	
        <td colspan="3">
        <input type="hidden"  name="closeall" value="0" type="hidden" />	
        <input type="checkbox" class="crimson-text" id="closeall" name="closeall" value="1">Close all openings for the selected semester</input>	
        </td>
        </tr>
        <tr/><tr/><tr/><tr/>
        
        <tr>
        				<td/>
                            <td colspan="2">
                              <input id="close" name="close" type="submit" onclick="return validate()" value="Close" />
                          </td>
                          
        </tr>
        </tbody>
        </table>
        </div>
        </div>
        <div id='expanderexc' style="margin-top:20px">
        <div>
        Delete an Opening -- Nobody can see this data
        </div>
        <div>
        <table cellspacing="5" cellpadding="5">
         <tbody>
        <tr/><tr/><tr/><tr/>
        <tr>
        <td/>	
        <td>
        <label class="crimson-text">Course</label>
        </td>
        <td>
       	<input type="text"	id="deletecourse" name="deletecourse" class="crimson-border" value="<?php echo $dl;?>" onkeypress="courseSuggest1()"/>		
        </td>
        </td><td class="error" >
     <?php  echo $deleteerr;?>
        </td>
        </tr>
        <tr/><tr/><tr/><tr/>
        <tr>
        <td/>	
        <td colspan="3">
        <input type="hidden" name="deleteall" value="0" class="crimson-text" ></input>	
        <input type="checkbox"  id="deleteall" name="deleteall" class="crimson-text" value="1" >Delete all openings for the selected semester</input>	
        </td>
        </tr>
        <tr/><tr/><tr/><tr/>
        
        <tr>
        				<td/>
                            <td colspan="2">
                              <input id="delete" name="delete" type="submit" value="Delete" onclick="return validate1()" />
                          </td>
                          
        </tr>
        </tbody>
        </table>
        </form>
        </div>
        </div>
	</div>

</body>
</html>